﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpAlgorithm
{
    class SortingGame
    {
        public SortingGame()
        {
            int testCase = Int32.Parse(TestConsole.ReadLine());
            while (testCase-- > 0)
            {
                int size = Int32.Parse(TestConsole.ReadLine());
                int[] firstArray = new int[size];
                string[] input = TestConsole.ReadLine().Split(' ');
                List<int> orderList = new List<int>();
                for (int i = 0; i < size; i++)
                {
                    firstArray[i] = Int32.Parse(input[i]);
                    orderList.Add(firstArray[i]);
                }
                //1~1,000,000 >>> 1~8
                orderList.Sort();
                for (int i = 0; i < size; i++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        if (firstArray[i] == orderList[j])
                        {
                            firstArray[i] = j + 1;
                        }
                    }
                }
                int firstValue = ArrayToInt(firstArray);
                int lastValue = Int32.Parse("12345678".Substring(0, size));
                Dictionary<int, int[]> result = new Dictionary<int, int[]>();

                result.Add(firstValue, new int[2] {0, -1});
                Queue<int[]> queue = new Queue<int[]>();
                queue.Enqueue(new int[2]{ firstValue, 0 });
                while(result.Count < 40321) //8!
                {
                    int[] value = queue.Dequeue();
                    if (value[0] == lastValue)
                    {
                        Console.WriteLine(value[1]);
                        break;
                    }
                    for (int i = 0; i < size; i++)
                    {
                        for (int j = i + 1; j < size; j++)
                        {
                            int[] nowArray = IntToArray(value[0], size);
                            Reverse(nowArray, i, j);
                            int nowValue = ArrayToInt(nowArray);
                            if (!result.ContainsKey(nowValue))
                            {
                                result.Add(nowValue, new int[2] {value[1] + 1, value[0]});
                                queue.Enqueue(new int[2]{nowValue, value[1] + 1});
                            }
                        }
                    }
                }
            }
        }
        private void Reverse(int[] array, int start, int end)
        {
            for (int i = 0; i < (end - start + 1) / 2; i++)
            {
                int buf = array[start + i];
                array[start + i] = array[end - i];
                array[end - i] = buf;
            }
        }
        private int[] IntToArray(int value, int size)
        {
            int[] array = new int[size];
            for (int i = size - 1; i >= 0; i--)
            {
                array[i] = value % 10;
                value /= 10;
            }
            return array;
        }
        private int ArrayToInt(int[] array)
        {
            int value = 0;
            int p = 1;
            for (int i = array.Length - 1; i >= 0; i--)
            {
                value += array[i] * p;
                p *= 10;
            }
            return value;
        }
    }
}
